﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRegimenMonitor.Common;
using WaterRegimenMonitor.Web.Domain;

namespace WaterRegimenMonitor.Web.Controllers
{
    /// <summary>
    /// 首页控制器。
    /// </summary>
    public class HomeController : Controller
    {
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        [ValidateInput(false)]
        public ActionResult Index()
        {
            //判断是否已经登录
            if (!this.Request.IsAuthenticated)
            {
                //如果没有则导航到登录画面
                return RedirectToAction("User", "Login");
            }

            //返回默认视图
            return View();
        }

        [HandleError(ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Logout()
        {
            //判断是否已经登录
            if (this.Request.IsAuthenticated)
            {
                //获得当前主题
                UserPrincipal principal = this.HttpContext.User as UserPrincipal;

                //执行登出操作
                Authentication.Logout();
            }

            //重定向到登录画面
            return Redirect("~/Login");
        }
    }
}
